<?php
session_start();
require_once("includes/header.php");
?>

<?php


if (!empty($_POST) && !empty($_SESSION['username'])) {
    echo "Welcome!   ";
    echo $_SESSION['username'];

    $values = array(
        "username" => $_SESSION['username'],
        "weight" => $_POST['weight'],
        "age" => $_POST['age'],
        "bmi" => $_POST['bmi'],
        "type" => $_POST['type'],
        "record" => $_POST['record'],
        "week_goal" => $_POST['goal'],
        'date' => date("Y-m-d h:i:sa"),
        'week' => date("W")
    );

    # read the json first, then insert data pair.
    $db = file_get_contents("data/user_stats.json");
    $db_array = json_decode($db, true);
    $db_array[] = $values;
    $json_obj = json_encode($db_array);
    file_put_contents("data/user_stats.json", $json_obj);

    # now process the log data
    $total_work = 0;
    foreach ($db_array as $key => $item) {
        if ($item['week'] == $values['week'] && $item['type'] == $values['type']) {
            $total_work = $total_work + $item['record'];
        }
    }
    $percentage = ($total_work / $values['week_goal']) * 100;

} else {
    echo "You need to log in. Now redirect to Login Page...";
    echo "<script language=\"JavaScript\">
window.setTimeout(\"location='login.php'\", 1500);
</script>";
}
?>

    <div class="container mx-auto">
        <div class="mx-auto text-center" style="400px;">
            <h2 class="text-center">Log book</h2>
        </div>
        <div class="col md-1">
            <div class="row">
                <div class="col">
                    <img src="assets/<?php echo $_POST['type'] ?>.png">
                </div>
                <div class="col">
                    <div class="row"><?php echo $_POST['type'] ?></div>
                    <div class="row"><?php echo $values['date'] ?></div>
                    <div class="row">
                        <p>You have exercised for <?php echo $values['record'] ?> minutes</p>
                        <p>Your weekly goal is <?php echo $values['week_goal'] ?>,total exercise this week is
                            <?php echo $total_work ?>. finished as <?php echo $percentage ?>%.
                        </p>
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="col">
                    <img src="assets/weight.png" width="104px" height="111px">
                </div>
                <div class="col">
                    <div class="row">Weight</div>
                    <div class="row"><?php echo $values['date'] ?></div>
                    <div class="row">
                        <p>You weight is <?php echo $values['weight'] ?> kg.</p>
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="col">
                    <img src="assets/bmi.png" width="104px" height="111px">
                </div>
                <div class="col">
                    <div class="row">BMI</div>
                    <div class="row"><?php echo $values['date'] ?></div>
                    <div class="row">
                        You BMI is <?php echo $values['bmi'] ?>.
                    </div>
                </div>
            </div>

        </div>
        <div class="col">

        </div>
    </div>

<?php
require_once("includes/footer.php");
?>